<?php
require_once 'auth.php';
require_once 'forward.php';
$user = $_SESSION['user'];

if (isset($_GET['rm'])) {
    remove_note($user, $_GET['rm']);
    $uri = $_SERVER['REQUEST_URI'];
    header("Location: http://".$_SERVER['HTTP_HOST'].substr($uri, 0, strpos($uri, "?")));
    exit;
}

if (isset($_GET['vi'])) {
    $edit_id = $_GET['vi'];
    $edit = load_note($user, $edit_id);
}

if (isset($_POST['newnotebody'])) {
    $subject = (isset($_POST['newnotesubject']) ? $_POST['newnotesubject'] : "");
    $body = $_POST['newnotebody'];
    $is_public = $_POST['newnotepublic'] == 'on';
    $uri = $_SERVER['REQUEST_URI'];
    $x = strpos($uri, "?");
    if ($x !== false) {
        $uri = substr($uri, 0, $x);
    }
    if (isset($edit)) {
        $note_id = update_existing_note($user, $edit_id, $subject, $body, $is_public);
        // Uncomment this to allow a user to stay on the note editing page after submitting changes
        // $uri = $uri."?vi=".$note_id;
    } else {
        add_new_note($user, $subject, $body, $is_public);
    }
    header("Location: http://".$_SERVER['HTTP_HOST'].$uri);
    exit;
}

render_begin(isset($edit) ? " :: edit" : "");
render_profile($user);
render_page_begin();
render_header();
if (isset($edit)) render_edit_notes($user, $edit, $edit_id);
    else render_all_notes($user, load_settings($user));
render_footer();
render_page_end();
render_end();

?>
